package com.mlnx.bptp.head;

import com.mlnx.bptp.Codec;
import com.mlnx.bptp.util.DecodeCRC16;

import java.nio.ByteBuffer;

/**
 * 协议头
 *
 * @author zzb
 * @create 2019/3/25 14:34
 */
public class Header implements Codec {
    public static final byte[] Heads = { (byte) 0xFa}; // 数据包包头字节

    private static final ByteBuffer buffer = ByteBuffer.allocate(30);
    private int length;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    @Override
    public void decode(ByteBuffer buf) {

    }

    @Override
    public byte[] encode() {
        buffer.clear();
        buffer.put(Heads);
        buffer.put((byte)length);
        buffer.flip();
        byte[] bs = new byte[buffer.remaining()];
        buffer.get(bs);

        return bs;
    }

    @Override
    public void init() {

    }
    public static void main(String[] args) throws Exception{
//        Header h=new Header();
//        h.setLength(10);
//        StringBuffer sb = new StringBuffer();
//        for(int i = 0; i < h.encode().length; i++) {
//            String hex = Integer.toHexString(h.encode()[i] & 0xFF);
//            if(hex.length() < 2){
//                sb.append(0);
//            }
//            sb.append(hex);
//        }
//
//        System.out.println( sb.toString());
        byte[] b={(byte)0xFA,(byte)0x06,(byte)0xa3,(byte)0x03};
        byte[] b1= DecodeCRC16.Crc16cal1(b);

    }
}
